<!DOCTYPE html>
<html>
<head>
<%@ include file="/resource.jsp"%>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta content="UTF-8" http-equiv="Content-Language">
<script type="text/javascript" src="${SYSTEM_PATH}/jlib/cometd/cometd.js"></script>
<script type="text/javascript" src="${SYSTEM_PATH}/jlib/cometd/cometd/AckExtension.js"></script>
<script type="text/javascript" src="${SYSTEM_PATH}/jlib/cometd/cometd/ReloadExtension.js"></script>
<script type="text/javascript" src="${SYSTEM_PATH}/js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="${SYSTEM_PATH}/jlib/cometd/jquery.cookie.js"></script>
<script type="text/javascript" src="${SYSTEM_PATH}/jlib/cometd/jquery.cometd.js"></script>
<script type="text/javascript" src="${SYSTEM_PATH}/jlib/cometd/jquery.cometd-reload.js"></script>
<script type="text/javascript" src="${SYSTEM_PATH}/js/comet.notify.js"></script>
<script type="text/javascript"> 
	function UUID(){  
	    this.id = this.createUUID();  
	};
	UUID.prototype.valueOf = function(){ return this.id; };  
	UUID.prototype.toString = function(){ return this.id; };  
	UUID.prototype.createUUID = function(){  
	    var dg = UUID.timeInMs(new Date(1582, 10, 15, 0, 0, 0, 0));
	    var dc = UUID.timeInMs(new Date());  
	    var t = dc - dg;  
	    var h = '';  
	    var tl = UUID.getIntegerBits(t,0,31);  
	    var tm = UUID.getIntegerBits(t,32,47);  
	    var thv = UUID.getIntegerBits(t,48,59) + '1'; // version 1, security version is 2  
	    var csar = UUID.getIntegerBits(UUID.randrange(0,4095),0,7);  
	    var csl = UUID.getIntegerBits(UUID.randrange(0,4095),0,7);  
	    var n = UUID.getIntegerBits(UUID.randrange(0,8191),0,7) +   
	            UUID.getIntegerBits(UUID.randrange(0,8191),8,15) +   
	            UUID.getIntegerBits(UUID.randrange(0,8191),0,7) +   
	            UUID.getIntegerBits(UUID.randrange(0,8191),8,15) +   
	            UUID.getIntegerBits(UUID.randrange(0,8191),0,15); // this last number is two octets long  
	    return tl + h + tm + h + thv + h + csar + csl + h + n;   
	};
	UUID.getIntegerBits = function(val,start,end){  
	    var base16 = UUID.returnBase(val,16);
	    var quadArray = new Array(); 
	    var quadString = '';  
	    var i = 0;  
	    for(i=0;i<base16.length;i++){  
	        quadArray.push(base16.substring(i,i+1));      
	    }  
	    for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){  
	        if(!quadArray[i] || quadArray[i] == '') quadString += '0';  
	        else quadString += quadArray[i];  
	    }
	    return quadString;  
	};  
	UUID.returnBase = function(number, base){  
	    var convert = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];  
	    if (number < base) var output = convert[number];
	    else {  
	        var MSD = '' + Math.floor(number / base);  
	        var LSD = number - MSD*base;  
	        if (MSD >= base) var output = this.returnBase(MSD,base) + convert[LSD];  
	        else var output = convert[MSD] + convert[LSD];
	    }  
	    return output;  
	};
	UUID.timeInMs = function(d){  
	    var ms_per_second = 100; // constant  
	    var ms_per_minute = 6000; // ms_per second * 60;  
	    var ms_per_hour   = 360000; // ms_per_minute * 60;  
	    var ms_per_day    = 8640000; // ms_per_hour * 24;  
	    var ms_per_month  = 207360000; // ms_per_day * 30;  
	    var ms_per_year   = 75686400000; // ms_per_day * 365;  
	    return Math.abs((d.getUTCFullYear() * ms_per_year) + (d.getUTCMonth() * ms_per_month) + (d.getUTCDate() * ms_per_day) + (d.getUTCHours() * ms_per_hour) + (d.getUTCMinutes() * ms_per_minute) + (d.getUTCSeconds() * ms_per_second) + d.getUTCMilliseconds());  
	};  
	UUID.randrange = function(min,max){  
	    var num = Math.round(Math.random() * max);  
	    if(num < min){   
	        num = min;  
	    } else if (num > max) {  
	        num = max;  
	    }  
	    return num;  
	}; 
    var config = {
        contextPath: '${SYSTEM_PATH}'
    };
	
	

	function join(userName){
		var opinion = {};
		opinion.url = location.protocol + "//" + location.host + config.contextPath + "/cometd";
		opinion.uuid = new UUID().createUUID();		
		opinion.username = userName;
		//opinion必须带有url、uuid、username 三个参数
		$.cometNotify.join(opinion);
	}
	
</script>
<link type="text/css" rel="stylesheet" href="${SYSTEM_PATH}/css/comet.chat.css"/>
</head>
<body><script type="text/javascript">
	var userName = '${username}';
	$(document).ready(function(){ 
		$.cometNotify.onLoad({memberListContainerID:'members'});
		join(userName);

		$("#notify").bind("click",function(){
			 $.cometd.publish('/notify/system', {
	                user: userName,
	                peer: "peer",
	                receivers:"user1",
	                message: "下雨啦，快收衣服？"
	          });
		});
	});
</script>
<input type="button" id="notify" value="notify">
<div>notify:</div>
<div id="notifyContent"></div>

</body>
</html>
